package com.chenyue.cm.payment.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.order.domain.JdProductType;
import com.chenyue.cm.order.domain.JdProductTypeExample;
import com.chenyue.cm.payment.service.JdProductTypeService;
import com.chenyue.cm.user.domain.JdAppUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author libingxing
 * @date 2021-07-07
 */
@RestController
@RequestMapping("/jdProductType")
public class JdProductTypeController {

    private final JdProductTypeService jdProductTypeService;

    public JdProductTypeController(JdProductTypeService jdProductTypeService) {
        this.jdProductTypeService = jdProductTypeService;
    }

//    @RequestMapping("/getList")
//    public Success getList(HttpServletRequest request) {
//        Pageable pageInfo = RequestUtil.getPageable(request);
//        PageVo<JdProductType> result = jdProductTypeService.getPageBean(pageInfo, JdProductType.class);
//        return Success.success(result);
//    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        //设置分页信息
        PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
        LambdaQueryWrapper<JdProductType> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(JdProductType::getName,"捷登体育-赛事报名");
        List<JdProductType> list = jdProductTypeService.list(wrapper);
        //封装返回结果
        PageInfo<JdProductType> result = new PageInfo<>(list);

        return Success.success(result);
    }


    /**
     * @param vo 插入实体
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(JdProductType vo) {
        boolean success = jdProductTypeService.save(vo);
        return success ? Success.success() : Success.error();
    }

    /**
     * @param vo      修改实体
     * @param request id
     * @return Success
     */
    @Log
    @RequestMapping("/edit")
    public Success edit(JdProductType vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            int success = jdProductTypeService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
            return Success.error("id不能为空");
        }
    }

    /**
     * @param id 主键
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        JdProductTypeExample aExample = new JdProductTypeExample();
        aExample.createCriteria().andIdEqualTo(id);
        int success = jdProductTypeService.deleteByExample(aExample);
        return success > 0 ? Success.success() : Success.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete")
    public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = jdProductTypeService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
    }

}
